package com.gdziarmaga.webshop.web.headers;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean {

	private static final long serialVersionUID = 1L;

	private String localeCode = "Polski";

	private static Map<String, Object> countries;
	static {
		countries = new LinkedHashMap<String, Object>();
		countries.put("English", Locale.ENGLISH);
		countries.put("Polski", new Locale("pl", "PL"));
	}

	public Map<String, Object> getCountriesInMap() {
		return countries;
	}

	public String getLocaleCode() {
		return localeCode;
	}

	public void setLocaleCode(String localeCode) {
		this.localeCode = localeCode;
		countryLocaleCodeChanged();
	}

	public void countryLocaleCodeChanged() {
		String newLocaleValue = localeCode;

		for (Map.Entry<String, Object> entry : countries.entrySet()) {

			if (entry.getKey().toString().equals(newLocaleValue)) {

				FacesContext.getCurrentInstance().getViewRoot()
						.setLocale((Locale) entry.getValue());
			}
		}
	}

}
